﻿#region Disclaimer/Info

/////////////////////////////////////////////////////////////////////////////////////////////////
//
//   File:		Item.cs
//   Website:		http://dexterblogengine.com/
//   Authors:		http://dexterblogengine.com/About.ashx
//   Rev:		1
//   Created:		19/01/2011
//   Last edit:		19/01/2011
//   License:		GNU Library General Public License (LGPL)
//   File:            Item.cs
//   For updated news and information please visit http://dexterblogengine.com/
//   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
//   For any question contact info@dexterblogengine.com
//
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Dexter.Domain.Model {
	/// <summary>
	/// 	Base class for posts and pages.
	/// 
	/// 	Remember to keep in sync the PageStructure class with this one
	/// </summary>
	public abstract partial class Item : EntityBase {
		string itemAbstract;

		protected Item ( ) {
			Tags = new Collection <TagItem> ( );
			Comments = new Collection <Comment> ( );
			Votes = new Collection <Vote> ( );
		}

		protected Item ( string title , string username , string formattedBody ) : this ( ) {
			Title = title;
			Username = username;
			FormattedBody = formattedBody;
		}

		public virtual string Title { get; set; }
		public virtual DateTime PublishDate { get; set; }
		public virtual bool IsPublished { get; protected set; }

		public virtual string Abstract {
			get {
				if (string.IsNullOrEmpty ( this.itemAbstract ))
					this.itemAbstract = this.FormattedBody.CleanHtmlText ( ).Cut ( 247, "..." ).Trim().DecodeHtml (  );
				
				return itemAbstract;
			}
			set { itemAbstract = value; }
		}

		public virtual bool BreakOnAggregate { get; set; }
		public virtual string FormattedBody { get; set; }
		public virtual string Slug { get; set; }
		public virtual bool CommentEnabled { get; protected set; }
		public virtual bool ShowOnAggregate { get; set; }
		public virtual bool ShowOnSearch { get; set; }
		public virtual int TotalComments { get; protected set; }
		public virtual int TotalTrackback { get; set; }
		public virtual string Username { get; set; }
		public virtual IEnumerable <Comment> Comments { get; protected set; }
		public virtual IEnumerable<Comment> ApprovedComments { get; protected set; }
		public virtual IEnumerable<Comment> UnApprovedComments { get; protected set; }

		public virtual IList <TagItem> Tags { get; protected set; }
		public virtual IEnumerable <Vote> Votes { get; protected set; }
		public virtual IEnumerable <TrackBack> TrackBacks { get; protected set; }

		/// <summary>
		/// Adds the category to this post.
		/// Inser the post in the category specified, the category MUST exists
		/// </summary>
		/// <param name="category">The category.</param>
		public virtual void AddCategory(Category category)
		{
			category.ValidateForAddToPost();
			Categories.Add(category);
		}

		public virtual IList<Category> Categories { get; set; }

		public virtual IList<Category> FlatCategories
		{
			get { return this.Categories.ToFlat(x => x.Categories).ToList(); }
		}

		public virtual string GetCommaSeparatedTags ( ) {
			return string.Join ( "," , Tags.Select ( x => x.Name ).ToArray ( ) );
		}
	}
}